<!--
 * @Author: zhoulei0232
 * @Date: 2022-12-08 00:37:02
 * @LastEditTime: 2023-01-11 21:23:10
 * @LastEditors: zhoulei0232
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
 
      <el-form-item>
 
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
 
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="painterList" @selection-change="handleSelectionChange"
      @sort-change="handleSortChange">

      <!-- <el-table-column label="序号" align="center" type="index" width="100" /> -->

      <el-table-column label="编号" align="left" prop="id" :show-overflow-tooltip="true" sortable />

      <el-table-column label="菜单中文名" align="center" prop="description" />

      <el-table-column label="菜单英文名" align="center" prop="name" />

      <el-table-column label="pid" align="center" prop="pid" />

      <el-table-column label="grade类型" align="center" prop="grade" />

      <el-table-column label="排序" align="center" prop="orders" />

      <el-table-column label="路径path" align="center" prop="path" />

      <el-table-column label="组件路径" align="center" prop="component"  width="250"/>

      <el-table-column label="菜单权限" align="center" prop="remark" />

      <el-table-column label="按钮权限" align="center" prop="permi" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

 

    <!-- 新增或修改数据 -->
    <el-dialog :title="title" v-model="open" width="480px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="22"  >
            <el-form-item label="id" prop="id">
              <el-input v-model="form.id"  placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="菜单中文名" prop="description">
              <el-input v-model="form.description" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="菜单英文名" prop="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="pid" prop="pid">
              <el-input v-model="form.pid" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="grade类型" prop="grade">
              <el-input v-model="form.grade" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="排序" prop="orders">
              <el-input v-model="form.orders" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="路径path" prop="path">
              <el-input v-model="form.path" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="组件路径" prop="component">
              <el-input v-model="form.component" placeholder="请输入" />
            </el-form-item>
          </el-col> 
          <el-col :span="22">
            <el-form-item label="图标Cls" prop="icon_cls">
              <el-input v-model="form.icon_cls" placeholder="请输入" />
            </el-form-item>
          </el-col> 
          <el-col :span="22">
            <el-form-item label="菜单权限" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入" />
            </el-form-item>
          </el-col> 
          <el-col :span="22">
            <el-form-item label="按钮权限" prop="permi">
              <el-input v-model="form.permi" placeholder="请输入" />
            </el-form-item>
          </el-col> 
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="debouncedFn">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


 
 

  </div>
</template>

<script setup name="Menu">
 

import {
  listAllMenu,
  updateMenu,
  addMenu
} from "@/api/business/menu";

import { userTypeEnums, stutasEnums } from "@/utils/enums"
import { useDebounceFn } from "@vueuse/core";
import { watch } from 'vue';

const { proxy } = getCurrentInstance();


 

const painterList = ref([]);
const dialogOpen1 = ref(false); // 导出表格

const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const open = ref(false); // 修改用户弹框
const dialogOpen2 = ref(false); // 导入表格
const fileList = ref([]);
const loadingBtn = ref(false);


const data = reactive({
  typeList: userTypeEnums,
  stutasList: stutasEnums,
  form: {},

  dialogForm1: {
    type: undefined,

  }, // 导出弹出框选择
  queryParams: {
    pageNum: 1,
    pageSize: 10,
 
    orderName: "",
    orderType: "DESC",
  },
  rules: {},
});

const { queryParams, form, rules,
  typeList,
  dialogForm1,
  stutasList 
  } =
  toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listAllMenu(queryParams.value).then((response) => {
    painterList.value = response.data;
    total.value = response.data.count;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  form.value = {}
  title.value = "新增";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改";
}

 

function handleSortChange(column, prop, order) {
  if (order == "descending") {
    data.queryParams.orderName = prop;
    data.queryParams.orderType = "DESC";
  } else if (order == "ascending") {
    data.queryParams.orderName = prop;
    data.queryParams.orderType = "ASC";
  } else {
    data.queryParams.orderName = "updatedAt";
    data.queryParams.orderType = "DESC";
  }
}
// 防抖操作
const debouncedFn = useDebounceFn(
  () => {
    submitForm();
  },
  1000,
  { maxWait: 3000 }
);
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (title.value != '新增') {
        updateMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
 
 


getList();

</script>
